在SeamlessM4T的開源程式碼中,其實還是有保留S2U(Speech-to-Unit)的功能,在audio_to_units的scripts中可以看出其架構,
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser(
description="Convert raw audio to units (and optionally audio) using UnitExtractor."
)
# 新增參數:音檔路徑
parser.add_argument("audio", type=str, help="Audio WAV file path.")
# 新增參數:kmeans分群模型
parser.add_argument(
"--kmeans_uri",
type=str,
help="URL path to the K-Means model.",
default="https://dl.fbaipublicfiles.com/seamlessM4T/models/unit_extraction/kmeans_10k.npy",
)
# 新增參數:xlsr2模型
parser.add_argument(
"--model_name",
type=str,
help="Feature extraction model name (`xlsr2_1b_v2`)",
default="xlsr2_1b_v2",
)
# 新增參數:第35層特徵提取層
parser.add_argument(
"--out_layer_number",
type=int,
help="Layer number of the feature extraction model to pull out features from.",
default=35,
)
args = parser.parse_args()
# 如果在Notebook使用要改用下一行
# args, unknown = parser.parse_known_args()
if torch.cuda.is_available():
device = torch.device("cuda:0")
logger.info("Running unit_extraction on the GPU.")
else:
device = torch.device("cpu")
logger.info("Running unit_extraction on the CPU.")
unit_extractor = UnitExtractor(args.model_name, args.kmeans_uri, device=device)
units = unit_extractor.predict(args.audio, args.out_layer_number - 1)
logger.info(f"Converted to units: {units}")
if __name__ == "__main__":
main()
從程式碼結構可以看出單元提取步驟,根據經驗決定計算預訓練的XLSRv2模型的第35層特徵,將每個時間步的特徵分配給預先計算的k-means分群模型,使用unit_extractor函式產生單位序列。